package lzzy.com;

import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class ThreadCooperation {
    private static Account account = new Account();

    public static void main(String[] args) {
        // Create a thread pool with two threads
        // 创建一个有两个线程的线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);
        executor.execute(new DepositTask());
        executor.execute(new WithdrawTask());
        executor.shutdown();

        System.out.println("Thread 1\t\tThread 2\t\tBalance");
    }
    private static class DepositTask implements Runnable {
        @Override
        public void run() {
            try {
                while (true) {
                    account.deposit((int)(Math.random() * 10) + 1);
                    Thread.sleep(1000);
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    private static class WithdrawTask implements Runnable {
        @Override
        public void run() {
                while (true) {
                    account.withdraw((int)(Math.random() * 10) + 1);
            }
        }
    }
    private static class Account {
        // Create a new lock  创建新锁
        private static Lock lock = new ReentrantLock();
        private static Condition newDeposit = lock.newCondition();
        private int balance = 0;
        public int getBalance(){
            return balance;
        }

        public void withdraw(int amount) {
            lock.lock();  // Acquire the lock
            try {
                while (balance < amount) {
                    System.out.println("\t\t\tWait for a deposit");
                    newDeposit.await();
                }

                balance -= amount;
                System.out.println("\t\t\tWithdraw" + amount + "\t\t" + getBalance());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();  // Release the lock
            }
        }
        public void deposit(int amount) {
            lock.lock();  // Acquire the lock
            try {
                balance += amount;
                System.out.println("Deposit" + amount + "\t\t\t\t\t" + getBalance());
                newDeposit.signalAll();
            }finally {
                lock.unlock();  // Release the lock
            }
        }
    }
}
